<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Boolean 布尔类型</title>
</head>
<div id="layout">
  <div id="layout-content"><div id="language.types.boolean" class="sect1">
 <h2 class="title">Boolean 布尔类型</h2>
 
 <p class="simpara">
  这是最简单的类型。<span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 表达了真值，可以为 <strong><code>TRUE</code></strong> 或 <strong><code>FALSE</code></strong>。
 </p>

 <div class="sect2" id="language.types.boolean.syntax">
  <h3 class="title">语法</h3>
  <p class="para">
    要指定一个布尔值，使用常量 <strong><code>TRUE</code></strong> 或 <strong><code>FALSE</code></strong>。两个都不区分大小写。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">True</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;设置&nbsp;$foo&nbsp;为&nbsp;TRUE<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   通常<a href="language.operators.html" class="link">运算符</a>所返回的
   <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 值结果会被传递给<a href="language.control-structures.html" class="link">控制流程</a>。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;==&nbsp;是一个操作符，它检测两个变量是否相等，并返回一个布尔值<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"show_version"</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;version&nbsp;is&nbsp;1.23"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;这样做是不必要的...<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$show_separators&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;hr&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;...因为可以使用下面这种简单的方式：<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$show_separators</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;hr&gt;\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>

 <div class="sect2" id="language.types.boolean.casting">
  <h3 class="title">转换为布尔值</h3>

  <p class="simpara">
   要明确地将一个值转换成 <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span>，用 <em>(bool)</em> 
   或者 <em>(boolean)</em> 来强制转换。但是很多情况下不需要用强制转换，因为当运算符，函数或者流程控制结构需要一个
   <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 参数时，该值会被自动转换。
  </p>

  <p class="simpara">
   参见<a href="language.types.type-juggling.html" class="link">类型转换的判别</a>。
  </p>
   
  <p class="para">
   当转换为 <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 时，以下值被认为是 <strong><code>FALSE</code></strong>：
  </p>
  
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.boolean.html" class="link">布尔</a>值 <strong><code>FALSE</code></strong> 本身
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.integer.html" class="link">整型</a>值 0（零）
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.float.html" class="link">浮点型</a>值 0.0（零）
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     空<a href="language.types.string.html" class="link">字符串</a>，以及<a href="language.types.string.html" class="link">字符串</a> &quot;0&quot;
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     不包括任何元素的<a href="language.types.array.html" class="link">数组</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     特殊类型 <a href="language.types.null.html" class="link">NULL</a>（包括尚未赋值的变量）
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     从空标记生成的 <a href="ref.simplexml.html" class="link">SimpleXML</a> 对象
    </span>
   </li>
  </ul>
    
  <p class="para">
   所有其它值都被认为是 <strong><code>TRUE</code></strong>（包括任何<a href="language.types.resource.html" class="link">资源</a> 和  <strong><code>NAN</code></strong>）。
  </p>
  
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    <em>-1</em> 和其它非零值（不论正负）一样，被认为是 <strong><code>TRUE</code></strong>！
   </p>
  </div>
  
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;-</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #0000BB">2.3e5</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;array(</span><span style="color: #0000BB">12</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;array());&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #DD0000">"false"</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

 </div>
</div></div></div></body></html>